package com.tanhua.server.controller;

import com.tanhua.model.enums.CommentSubject;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

import static com.tanhua.model.enums.CommentType.LIKE;
import static com.tanhua.model.enums.CommentType.LOVE;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private CommentsService commentsService;

    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 点赞/取消点赞/喜欢/取消喜欢
     * @param subjectId 主体id(动态ID|评论ID|小视频ID)
     * @param dispose 操作(点赞|取消点赞|喜欢|取消喜欢)
     * @return
     */
    @PostMapping("/{id}/{dispose}")
    public ResponseEntity like(@PathVariable("id") String subjectId,@PathVariable("dispose") String dispose) {
        //正向还是反向操作（正向：点赞|喜欢  反向：喜欢|取消喜欢）
        boolean isFront =  "like".equals(dispose) || "love".equals(dispose);
        CommentType commentType = "like".equals(dispose) || "dislike".equals(dispose) ? LIKE:LOVE;

        Integer likeCount = commentsService.disposeComment(subjectId,isFront,commentType, CommentSubject.VIDEO);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 分页查询评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity findComments(@PathVariable("id") String videoId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = commentsService.findComments(videoId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 发布评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@PathVariable("id") String videoId,@RequestBody Map map) {
        String comment = (String)map.get("comment");
        commentsService.saveComments(videoId,comment,CommentSubject.VIDEO);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论点赞
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity like(@PathVariable("id") String commentId) {
        Integer likeCount = commentsService.disposeComment(commentId, true, CommentType.LIKE, CommentSubject.COMMENT);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 评论取消点赞
     */
    @PostMapping("comments/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String commentId) {
        Integer likeCount = commentsService.disposeComment(commentId, false, CommentType.LIKE, CommentSubject.COMMENT);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 关注作者
     * @param followUserId 作者ID
     * @return
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity userFocus(@PathVariable("id") Long followUserId){
        this.videosService.userFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注作者
     * @param followUserId 作者ID
     * @return
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("id") Long followUserId){
        this.videosService.userUnFocus(followUserId);
        return ResponseEntity.ok(null);
    }
}